package com.infoland.afanmall.model.xilie.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.layoutManager.GridSpacingItemDecoration;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.xilie.entity.XiLieListEntity;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 一刀
 * @date 2018/8/8 13:04
 * 系列商品列表
 */
public class XiLieListActivity extends AppCompatActivity {
    @BindView(R.id.xi_lie_list_recycler)
    RecyclerView xiLieRecycler;
    //系列商品数据
    List<XiLieListEntity> xiLieListEntities;
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    @BindView(R.id.tian_chong)
    LinearLayout tianchong;
    @BindView(R.id.xi_lie_refresh)
    RefreshLayout xiLieRefresh;
    //返回按钮
    @BindView(R.id.info_back)
    ImageView backBtn;

    private int getLayout() {
        return R.layout.xi_lie_list_activity;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        //获取数据
        initData();
        //设置不能上拉加载
        xiLieRefresh.setEnableLoadMore(false);
        //刷新数据
        xiLieRefresh.setOnRefreshListener(refreshLayout -> {
            initData();
        });
        xiLieRecycler.setFocusableInTouchMode(false);
        //沉浸式布局
        StatusBarKit.immersiveBlackTitleText(this, titleLayout);
        //设置脚步填充高度
        StatusBarKit.setPadding(this,tianchong);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplication(), 2);
        xiLieRecycler.setLayoutManager(gridLayoutManager);
        xiLieRecycler.addItemDecoration(new GridSpacingItemDecoration(2, SizeUtils.dp2px(10), true));

    }

    @Override
    protected void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }

    //初始化数据
    private void initData(){
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("response200 ---->   ", "  " + response200);
                    xiLieListEntities = JSONArray.parseArray(response200, XiLieListEntity.class);
                    initAdapter();
                    xiLieRefresh.finishRefresh(500);
                },
                error401 -> {
                    LogKit.E("success401 ---->   ", "  " + error401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.GET_XI_LIE_LIST + UserKit.getAccessToken(), new JSONObject(), this);
    }
    //添加适配器
    private void initAdapter(){
        xiLieRecycler.setAdapter(new BaseQuickAdapter<XiLieListEntity, BaseViewHolder>(R.layout.xi_lie_list_item, xiLieListEntities) {
            @Override
            protected void convert(BaseViewHolder helper, XiLieListEntity item) {
                LogKit.E("item"," "+item.toString());
                //图片
                AppCompatImageView appCompatImageView = helper.getView(R.id.xi_lie_item_image);
                ToolsKit.setImgView(item.getGs_image(), appCompatImageView);
                //查看详细按钮点击事件
                helper.getView(R.id.xi_lie_xiang_xi).setOnClickListener(v -> {
                    Bundle bunlde = new Bundle();
                    bunlde.putInt("id", item.getId());
                    ToolsKit.goDh(XiLieListActivity.this, XiLieActivity.class, bunlde);
                });
            }
        });
    }
    //返回按钮
    private void goBackBtn(){
        backBtn.setOnClickListener(v -> {
            finish();
            ToolsKit.goCloseDh(this);
        });
    }
}
